package com.example.nongye.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jaeger.library.StatusBarUtil;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.impl.ConfirmPopupView;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.example.nongye.R;
import com.example.nongye.databinding.ActivityFilterBinding;
import com.example.nongye.utils.DateUtils;
import com.example.nongye.utils.PXUtil;

import java.util.Calendar;
import java.util.Date;

public class FilterActivity extends AppCompatActivity {

    private ActivityFilterBinding binding;

    private Calendar rangStartCalendar, startDateCalendar, endDateCalendar;

    private TimePickerView startTPV, endTpv;

    private ConfirmPopupView confirmPopupView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFilterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
        StatusBarUtil.setLightMode(this);

        binding.btnBack.setOnClickListener(view -> finish());

        rangStartCalendar = Calendar.getInstance();
        startDateCalendar = Calendar.getInstance();
        endDateCalendar = Calendar.getInstance();

        Date rangStartDate = DateUtils.stringToDate("2016-01-01", DateUtils.DATE_FORMAT);
        rangStartCalendar.setTime(rangStartDate);


        if (null != getIntent().getExtras()) {
            String startDate = getIntent().getExtras().getString("start_date");
            String endDate = getIntent().getExtras().getString("end_date");
            binding.tvStartDate.setText(startDate);
            binding.tvEndDate.setText(endDate);
            startDateCalendar.setTime(DateUtils.stringToDate(startDate, DateUtils.DATE_FORMAT));
            endDateCalendar.setTime(DateUtils.stringToDate(endDate, DateUtils.DATE_FORMAT));
        }

        binding.btnSure.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("start_date", binding.tvStartDate.getText().toString());
            intent.putExtra("end_date", binding.tvEndDate.getText().toString());
            setResult(Activity.RESULT_OK, intent);
            finish();
        });

        binding.tvStartDate.setOnClickListener(view -> {
            if (null != startTPV) {
                startTPV.setDate(startDateCalendar);
                startTPV.show();
            } else {
                initStartTPV();
            }
        });

        binding.tvEndDate.setOnClickListener(view -> {
            if (null != endTpv) {
                endTpv.setDate(endDateCalendar);
                endTpv.show();
            } else {
                initEndTPV();
            }
        });
    }

    private void initStartTPV() {
        startTPV = new TimePickerBuilder(FilterActivity.this, (date, v) -> {//选中事件回调
            if (DateUtils.isAfter(date, endDateCalendar)) {
                binding.tvStartDate.setText(DateUtils.dateToString(date, DateUtils.DATE_FORMAT));
                startDateCalendar.setTime(date);
            } else {
                //弹框提示
                showTip();
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})//默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setContentTextSize(20)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(getResources().getColor(R.color.colorAccent))//确定按钮文字颜色
                .setCancelColor(Color.BLACK)//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.background))//标题背景颜色 Night mode
                .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
                .setRangDate(rangStartCalendar, endDateCalendar)//起始终止年月日设定,默认是1900-2100年
                .setDate(startDateCalendar)// 如果不设置的话，默认是系统时间*/
                .setLabel("年", "月", "日", "时", "分", "秒")
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setLineSpacingMultiplier(2.0f)//行间距倍数
                .setItemVisibleCount(5)
                .build();

        LinearLayout timepicker = startTPV.getDialogContainerLayout().findViewById(R.id.timepicker);
        timepicker.setMinimumHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 235, getResources().getDisplayMetrics()));

        startTPV.show();
    }

    private void initEndTPV() {
        endTpv = new TimePickerBuilder(FilterActivity.this, (date, v) -> {//选中事件回调
            if (DateUtils.isAfter(startDateCalendar, date)) {
                binding.tvEndDate.setText(DateUtils.dateToString(date, DateUtils.DATE_FORMAT));
                endDateCalendar.setTime(date);
            } else {
                showTip();
            }

        })
                .setType(new boolean[]{true, true, true, false, false, false})//默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setContentTextSize(20)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(getResources().getColor(R.color.colorAccent))//确定按钮文字颜色
                .setCancelColor(Color.BLACK)//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.background))//标题背景颜色 Night mode
                .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
                .setRangDate(rangStartCalendar, endDateCalendar)//起始终止年月日设定,默认是1900-2100年
                .setDate(endDateCalendar)// 如果不设置的话，默认是系统时间*/
                .setLabel("年", "月", "日", "时", "分", "秒")
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setLineSpacingMultiplier(2.0f)//行间距倍数
                .setItemVisibleCount(5)
                .build();
        LinearLayout timepicker = endTpv.getDialogContainerLayout().findViewById(R.id.timepicker);
        timepicker.setMinimumHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 235, getResources().getDisplayMetrics()));
        endTpv.show();
    }


    private void showTip() {
        if (null != confirmPopupView) {
            confirmPopupView.show();
            return;
        }
        confirmPopupView = new XPopup.Builder(this)
                .autoDismiss(true)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .setPopupCallback(new SimpleCallback() {
                    @Override
                    public void onShow(BasePopupView popupView) {
                        super.onShow(popupView);
                        confirmPopupView.getContentTextView().setTextColor(getResources().getColor(R.color.color_666));
                        confirmPopupView.getContentTextView().setHeight(PXUtil.dip2px(FilterActivity.this, 50));
                        confirmPopupView.getConfirmTextView().setTextColor(getResources().getColor(R.color.colorAccent));
                        confirmPopupView.getConfirmTextView().setTypeface(Typeface.DEFAULT_BOLD);
                    }
                })
                .asConfirm("提示",
                        "起始日期不能晚于结束日期",
                        "",
                        "关闭",
                        null,
                        null, true);
        confirmPopupView.show();
    }
}